/**
 * 
 */
package fhq.hcmute.modernshopping.fragment;

import java.lang.reflect.Type;
import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import fhq.hcmute.modernshopping.ProjectUtil;
import fhq.hcmute.modernshopping.R;
import fhq.hcmute.modernshopping.adapter.ListStoreAdapter;
import fhq.hcmute.modernshopping.adapter.Product;
import fhq.hcmute.modernshopping.adapter.Store;
import fhq.hcmute.modernshopping.webservice.Webservice;

/**
 * @author: Hien Nguyen Le
 * @emai: hien.nguyenle.it@gmail.com
 * 
 */
public class ProductInStores extends Fragment {
	private static ProductInStores instance = null;
	private ListView lv_stores;
	private ArrayList<Store> listStores;

	private ProductInStores() {

	}

	public static ProductInStores getProductInStores(Activity activity,
			Product product) {
		if (instance == null) {
			instance = new ProductInStores();
			String url = ProjectUtil.WEB_SERVICE_LINK + "ListStore/%d";
			url = String.format(url, product.getCode());
			new Webservice(activity, instance.productInStoresHandler)
					.execute(url);
		}
		return instance;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.fragment_product_in_stores,
				container, false);
		lv_stores = (ListView) root.findViewById(R.id.lv_stores);
		lv_stores.setOnItemLongClickListener(onStoreItemLongClickListener);
		this.registerForContextMenu(lv_stores);
		if (instance != null && listStores != null) {
			ListStoreAdapter listStoreAdapter = new ListStoreAdapter(
					ProductInStores.instance.getActivity(), listStores);
			lv_stores.setAdapter(listStoreAdapter);
		}
		return root;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		ProductInStores.instance.getActivity().getMenuInflater()
				.inflate(R.menu.context_menu_display_product_details, menu);
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		Store store = (Store) lv_stores.getTag();
		String uri = null;
		if (item.getItemId() == R.id.menu_item_show_place) {
			uri = String
					.format(ProjectUtil.GOOGLE_GEOCODE, store.getLatitude(),
							store.getLongtitude(), store.getLatitude(),
							store.getLongtitude(), store.getName());
		} else if (item.getItemId() == R.id.menu_item_direction) {
			uri = String.format(ProjectUtil.GOOGLE_DIRECTION,
					store.getLatitude(), store.getLongtitude());
		}
		if (uri != null) {
			Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
			startActivity(intent);
		}
		return super.onContextItemSelected(item);
	}

	public static void destroyInstance() {
		ProductInStores.instance = null;
	}

	private OnItemLongClickListener onStoreItemLongClickListener = new AdapterView.OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int position, long arg3) {
			Store store = listStores.get(position);
			lv_stores.setTag(store);
			return false;
		}
	};

	private Handler productInStoresHandler = new Handler() {

		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == Webservice.GET_DATA_SUCCESSFUL) {
				String result = (String) msg.obj;
				Gson gson = new Gson();
				Type collectionType = new TypeToken<ArrayList<Store>>() {
				}.getType();
				ProductInStores.instance.listStores = (ArrayList<Store>) gson
						.fromJson(result, collectionType);
				ListStoreAdapter listStoreAdapter = new ListStoreAdapter(
						ProductInStores.instance.getActivity(),
						ProductInStores.instance.listStores);
				if (listStoreAdapter != null) {
					ProductInStores.instance.lv_stores
							.setAdapter(listStoreAdapter);
				}
			} else if (msg.what == Webservice.GET_DATA_FAILED) {

			}
			super.handleMessage(msg);
		}
	};
}
